﻿using System;
using System.Collections.Generic;
using System.Device.Location;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Awaken.Utils.WidgetCode
{
   public class LocationHelp
   { 
        const double pi = 3.14159265358979324;
        const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
        /// <summary>
        /// 中国正常坐标系GCJ02协议的坐标，转到 百度地图对应的 BD09 协议坐标
        /// </summary> 
        public static void Convert_GCJ02_To_BD09( double gcjLat,  double gcjLng ,out double bdLat,out double bdLng)
        {
            double x = gcjLng, y = gcjLat;
            double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * x_pi);
            double theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * x_pi);
            bdLng = z * Math.Cos(theta) + 0.0065;
            bdLat = z * Math.Sin(theta) + 0.006;
        }
        /// <summary>
        /// 百度地图对应的 BD09 协议坐标，转到 中国正常坐标系GCJ02协议的坐标
        /// </summary> 
        public static void Convert_BD09_To_GCJ02( double bdLat,  double bdLng,out double gcjLat,out double gcjLng)
        {
            double x = bdLng - 0.0065, y = bdLat - 0.006;
            double z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * x_pi);
            double theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * x_pi);
            gcjLng = z * Math.Cos(theta);
            gcjLat = z * Math.Sin(theta);
        }
         
        public static void Convert_WGS84_To_GCJ02(double wgLat, double wgLng, out double gjcLat, out double gjcLng)
        {
            if (OutOfChina(wgLat, wgLng))
            {
                gjcLat = wgLat;
                gjcLng = wgLng;
                return;
            }
            double dLat = TransformLat(wgLng - 105.0, wgLat - 35.0);
            double dLon = TransformLon(wgLng - 105.0, wgLat - 35.0);
            double radLat = wgLat / 180.0 * pi;
            double magic = Math.Sin(radLat);
            magic = 1 - ee * magic * magic;
            double sqrtMagic = Math.Sqrt(magic);
            dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
            dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi);
            gjcLat = wgLat + dLat;
            gjcLng = wgLng + dLon;
        }

        public static void Convert_WGS84_To_BD09(double lat, double lng, out double bdLat, out double bdLng)
        {
            double gcjLat;
            double gcjLng; 
            Convert_WGS84_To_GCJ02(lat, lng, out gcjLat, out gcjLng); 
            Convert_GCJ02_To_BD09(gcjLat, gcjLng, out bdLat, out bdLng);
        }
       /// <summary>
       /// 取两坐标点距离
       /// </summary>
       /// <param name="lat1"></param>
       /// <param name="lng1"></param>
       /// <param name="lat2"></param>
       /// <param name="lng2"></param>
       /// <returns></returns>
        public static double GetDistance(double lat1, double lng1, double lat2, double lng2)
        {
            var loa1 = new GeoCoordinate(lat1, lng1);
            var loa2 = new GeoCoordinate(lat2, lng2);
            var distanceM = loa1.GetDistanceTo(loa2);
            var distance = Math.Round(distanceM / 1000, 1, MidpointRounding.AwayFromZero);
            return distance;
        }
         
        // Krasovsky 1940
        //
        // a = 6378245.0, 1/f = 298.3
        // b = a * (1 - f)
        // ee = (a^2 - b^2) / a^2;
        const double a = 6378245.0;
        const double ee = 0.00669342162296594323;


        static bool OutOfChina(double lat, double lon)
        {
            if (lon < 72.004 || lon > 137.8347)
                return true;
            if (lat < 0.8293 || lat > 55.8271)
                return true;
            return false;
        }

        static double TransformLat(double x, double y)
        {
            double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.Sqrt(Math.Abs(x));
            ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
            ret += (20.0 * Math.Sin(y * pi) + 40.0 * Math.Sin(y / 3.0 * pi)) * 2.0 / 3.0;
            ret += (160.0 * Math.Sin(y / 12.0 * pi) + 320 * Math.Sin(y * pi / 30.0)) * 2.0 / 3.0;
            return ret;
        }

        static double TransformLon(double x, double y)
        {
            double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.Sqrt(Math.Abs(x));
            ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
            ret += (20.0 * Math.Sin(x * pi) + 40.0 * Math.Sin(x / 3.0 * pi)) * 2.0 / 3.0;
            ret += (150.0 * Math.Sin(x / 12.0 * pi) + 300.0 * Math.Sin(x / 30.0 * pi)) * 2.0 / 3.0;
            return ret;
        }
   
    }
}
